/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JF_Mysql.java
 *
 * 
 */
package SistemaBackup;

import java.io.File;
import java.sql.Connection;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * 
 */
public class JF_Mysql extends javax.swing.JFrame {
Connection con = null; //variável para usar em conexão de banco de dados.

    /** Creates new form JF_Mysql */
    public JF_Mysql() {
        initComponents();
        try {
            con = Conexao.conectar();//Conecta ao banco de dados 
            JFC_Salvar_Backup.setVisible(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e, "Erro!", 2);
        }        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jFileChooser1 = new javax.swing.JFileChooser();
                JB_Backup = new javax.swing.JButton();
                JFC_Salvar_Backup = new javax.swing.JFileChooser();
                jLabel1 = new javax.swing.JLabel();
                jLabel2 = new javax.swing.JLabel();
                JB_Backup2 = new javax.swing.JButton();
                jLabel3 = new javax.swing.JLabel();

                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

                JB_Backup.setText(" Backup ");
                JB_Backup.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                JB_BackupActionPerformed(evt);
                        }
                });

                JFC_Salvar_Backup.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
                JFC_Salvar_Backup.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                JFC_Salvar_BackupActionPerformed(evt);
                        }
                });

                jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 30)); // NOI18N
                jLabel1.setText(" Sistema de Backup LBC");

                jLabel2.setText("*Backup de todos os bancos de dados");

                JB_Backup2.setText("Backup");
                JB_Backup2.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                JB_Backup2ActionPerformed(evt);
                        }
                });

                jLabel3.setText("*Backup de bancos de dados Específicos");

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(JFC_Salvar_Backup, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(JB_Backup)
                                                        .addComponent(JB_Backup2))
                                                .addGap(18, 18, 18)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLabel1)
                                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(0, 170, Short.MAX_VALUE)))
                                .addContainerGap())
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(JB_Backup)
                                        .addComponent(jLabel2))
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(JB_Backup2)
                                        .addComponent(jLabel3))
                                .addGap(106, 106, 106)
                                .addComponent(JFC_Salvar_Backup, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                );

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void JB_BackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_BackupActionPerformed
        // Botão Backup
        try {
String arquivo = null;

JFC_Salvar_Backup.setVisible(true);                        

int result = JFC_Salvar_Backup.showSaveDialog(null);

if(result == JFileChooser.APPROVE_OPTION){
arquivo = JFC_Salvar_Backup.getSelectedFile().toString().concat(".sql");

File file = new File(arquivo); 

if(file.exists()){
   Object[] options = { "Sim", "Não" };
                int opcao = JOptionPane.showOptionDialog(null,"Este arquivo já existe. Quer sobreescrever este arquivo?", "Atenção!!!",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);
                    if (opcao == JOptionPane.YES_OPTION) {                
                        Runtime bck = Runtime.getRuntime();   
				bck.exec(" mysqldump -h $SERVER -v -v -v --host=localhost --user=root --password=!MaN@NbI --port=3306 --protocol=tcp --force --allow-keywords --compress  --add-drop-table --default-character-set=latin1 --hex-blob  --result-file="+arquivo+" --all-databases");  
                        JOptionPane.showMessageDialog(null, "Backup realizado com sucesso.", "Tudo OK!", 1);
                    }else{
                        JB_BackupActionPerformed(evt);
                    }
}else{

                        Runtime bck = Runtime.getRuntime();   
                        bck.exec("mysqldump -h $SERVER -v -v -v --host=localhost --user=root --password=!MaN@NbI --port=3306 --protocol=tcp --force --allow-keywords --compress  --add-drop-table --default-character-set=latin1 --hex-blob  --result-file="+arquivo+" --all-databases"); 
			//Para fazer backup de um banco só --databases nomebanco
			
                        JOptionPane.showMessageDialog(null, "Backup realizado com sucesso.", "Tudo OK!", 1);   
}

}
    
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e, "Erro!", 2);
        }

    }//GEN-LAST:event_JB_BackupActionPerformed

        private void JFC_Salvar_BackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JFC_Salvar_BackupActionPerformed
                // TODO add your handling code here:
        }//GEN-LAST:event_JFC_Salvar_BackupActionPerformed

        private void JB_Backup2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_Backup2ActionPerformed
                                                       
        // Botão Backup2
        try {
String arquivo = null;

JFC_Salvar_Backup.setVisible(true);                        

int result = JFC_Salvar_Backup.showSaveDialog(null);

if(result == JFileChooser.APPROVE_OPTION){
arquivo = JFC_Salvar_Backup.getSelectedFile().toString().concat(".sql");

File file = new File(arquivo); 

if(file.exists()){
   Object[] options = { "Sim", "Não" };
                int opcao = JOptionPane.showOptionDialog(null,"Este arquivo já existe. Quer sobreescrever este arquivo?", "Atenção!!!",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);
                    if (opcao == JOptionPane.YES_OPTION) {                
                        Runtime bck = Runtime.getRuntime();   
                        bck.exec(" mysqldump -h $SERVER -v -v -v --host=localhost --user=root --password=!MaN@NbI --port=3306 --protocol=tcp --force --allow-keywords --compress  --add-drop-table --default-character-set=latin1 --hex-blob  --result-file="+arquivo+" --databases Sistema_Backup");  
                        JOptionPane.showMessageDialog(null, "Backup realizado com sucesso.", "Tudo OK!", 1);
                    }else{
                        JB_BackupActionPerformed(evt);
                    }
}else{
 
                        Runtime bck = Runtime.getRuntime();   
                        bck.exec("mysqldump -h $SERVER -v -v -v --host=localhost --user=root --password=!MaN@NbI --port=3306 --protocol=tcp --force --allow-keywords --compress  --add-drop-table --default-character-set=latin1 --hex-blob  --result-file="+arquivo+" --databases Sistema_Backup"); 
			//Para fazer backup de um banco só --databases nomebanco
			
                        JOptionPane.showMessageDialog(null, "Backup realizado com sucesso.", "Tudo OK!", 1);   
}

}
    
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e, "Erro!", 2);
        }

                                            // TODO add your handling code here:
        }//GEN-LAST:event_JB_Backup2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JF_Mysql().setVisible(true);
            }
        });
    }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton JB_Backup;
        private javax.swing.JButton JB_Backup2;
        private javax.swing.JFileChooser JFC_Salvar_Backup;
        private javax.swing.JFileChooser jFileChooser1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        // End of variables declaration//GEN-END:variables
}
